//
//  sprite.cpp
//  gles1
//
//  Created by Realore Realore on 27.09.11.
//  Copyright 2011 Realore Studios. All rights reserved.
//

#include "sprite.h"
#include "engine.h"
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>

#import "timer.h"

 // Date constructor



//OGLSprite========================================

void OGLSprite::Zoom(float x1,float y1,float x2, float y2){
    //
    //float rratio;
    
    float fingersX,fingersY;
    float imgcenterX,imgcenterY;
    float diffx;
    float diffy;
    
    if(drag!=true){
        NSLog(@"Star Drug!");
        drag=true;
        currentDist = sqrtf(powf(((width)-0),2)+powf(((height)-0), 2));
        //currentDist=abs(currentDist);
        currentDistPoints = sqrtf(powf((x2-x1),2)+powf((y2-y1), 2));
        //NSLog(@"Distances at start: %f and %f",currentDist,currentDistPoints);
        rratio = currentDist/currentDistPoints;
        
        currCenterX=(((x2-x1)/2)+x1)-((width/2)+x);
        currCenterY=(((y2-y1)/2)+y1)-((height/2)+y);
        //        
        fingersX=((x2-x1)/2)+x1;
        fingersY=((y2-y1)/2)+y1;
        
        imgcenterX=(width/2)+x;
        imgcenterY=(height/2)+y;
        
        diffx=imgcenterX-fingersX;
        diffy=imgcenterY-fingersY;
        //
    }

    
    
    //NSLog(@"rect x:%f x:%f",x2, y2);
    float Ax,Ay,Bx,By;
    
    dist1 = sqrtf(powf((x2-x1),2)+powf((y2-y1), 2));//real dist between points
    dist1 = dist1*0.6;
    
    Ax=x1;
    Ay=y1;
    
    
    
    
    Bx=Ax+(x2-Ax)/2;//center between fingers
    By=Ay+(y2-Ay)/2; 
    
    float dist;
        
    dist=dist1*rratio;//zoom        
    SetPolygon((dist*ratio), dist);//zoom
    Move(Bx-(width/2)-currCenterX, By-(height/2)-currCenterY);
    
    /*
    Ax=x1;
    Ay=y1;
    //
    Bx=x2;
    By=y2;
    //
    
        
    
    //
    float newWidth,newHeight;
    newWidth = Ax - Bx;
    newHeight=Ay - By;
        
    x=Ax;
    y=Ay;
    
    float aspectratio=newWidth/newHeight;

    if(aspectratio>=1.3333){
        newHeight=By-Ay;
        newWidth=newHeight*1.3333;
    }else{
        newWidth=Bx-Ax;
        newHeight=newWidth/1.3333;
    }
        
    SetPolygon(newWidth, newHeight);
    */
    
    
}

bool OGLSprite::HitPointTest(float _x,float _y){
    //NSLog(@"Hit test");
    if(_x>=x && _x<=x+width && _y>=y && _y<=y+height){
        //NSLog(@"Hit!");
        return true;
    }
    return false;
}

 OGLSprite::OGLSprite(int _width, int _height,int _x,int _y, Texture& tex)
 {
     InitTexture(tex);
     TextureID = 0;
     rotation = 0;
     x=_x;
     y=_y;
     //frames init
     totalFrames = 16; 
     currFrame = 1;
     elapse=0;
     animSpeed=10;
     playing = false;
     //geometry
     
     SetPolygon(_width, _height);
ratio = width/height;
     SetTextureCoords(0, 0, 1, 1);
     
     //m_pTimer = new CTimer(this,50,NULL,false);
     
 }

void OGLSprite::InitTexture(char filename[]){ 
    spriteTexture = Texture(filename);
}
//create texture from another texture
void OGLSprite::InitTexture(Texture& tex){
    spriteTexture = Texture(tex.Tex);
    spriteTexture.width = tex.width;
    spriteTexture.height = tex.height;
}

void OGLSprite::Move(int _x, int _y){
    //NSLog(@"Move Sprite: %f  y:%f",x,y);
    x=_x;
    y=_y;
}

void OGLSprite::Update(){
    //update anim
    if(playing==true){
    elapse+=10;
    
    if(elapse>=animSpeed){
        elapse=0;        
        currFrame++;    
        if(currFrame>totalFrames-1)currFrame=0;
        SetFrame(currFrame);

        }
    }
}

void OGLSprite::SetFrame(int frame){
    currFrame = frame;
    float frameu,framev;
    frameu = spriteTexture.width / width;
    framev =totalFrames = spriteTexture.height / height;
    if(frame<=totalFrames){
        //float ufactor = 1/frameu;
        float vfactor = 1/framev;
        SetTextureCoords(0, vfactor*currFrame, 1, vfactor*(currFrame+1));
    }
}

void OGLSprite::Play(){
    elapse=0;//sets timer to 0
    playing = true;//and enables anim update
    
}

void OGLSprite::Stop(){
    playing=false;
    elapse=0;    
}

void OGLSprite::GotoAndPlay(int frame){
    currFrame = (frame);
    SetFrame(currFrame);
    Play();    
}

void OGLSprite::GotoAndStop(int frame){
    currFrame = (frame);
    SetFrame(currFrame);
    Stop();    
}




//
void OGLSprite::SetPolygon(float _width, float _height){
    //NSLog(@"set pilygon:%f height:%f",_width, _height);
    width = _width;
    height = _height;
    squareVert[0] =         0.0f;
    squareVert[1] =         0.0f;
    squareVert[2] =         width;
    squareVert[3] =         0.0f;
    squareVert[4] =         0.0f;
    squareVert[5] =         -height;
    squareVert[6] =         width;
    squareVert[7] =         -height;
}
void OGLSprite::SetTextureCoords(float _u1,float _v1,float _u2,float _v2){
    //texturing according to frames
    u1=_u1;
    v1=_v1;
    u2=_u2;
    v2=_v2;
    
    //
    texCoord[0] =      u1;
    texCoord[1] =      v2;
    
    texCoord[2] =      u2;
    texCoord[3] =      v2;
    
    texCoord[4] =      u1;
    texCoord[5] =      v1;
    
    texCoord[6] =      u2; 
    texCoord[7] =      v1;
}

void OGLSprite::onTimer(const CTimer* theTimer)
{
    currFrame++;    
    if(currFrame>totalFrames-1)currFrame=0;
    SetFrame(currFrame);
}

void OGLSprite::UpdateAnim(){

}

void  OGLSprite::Draw(){
    
    glMatrixMode(GL_MODELVIEW);//change to modelview
    glLoadIdentity();//reset, for absolute position of sprite
    glTranslatef(x, 480-y, 0);//move sprite to needed coords
    
    glRotatef(rotation, 0.0, 0.0, 1.0);
    
    //if(CURRENTTEXTURE!=spriteTexture.Tex){
        glBindTexture(GL_TEXTURE_2D, spriteTexture.Tex);
        //CURRENTTEXTURE=spriteTexture.Tex;
    //}

    //

    glVertexPointer(2, GL_FLOAT, 0, squareVert);//define vertexes
    glEnableClientState(GL_VERTEX_ARRAY);//change state to vertexarray for drawing from array
    
        
    glTexCoordPointer(2, GL_FLOAT, 0, texCoord);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
   	//draw
    
    try {
        
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);//draw arrays 

    }
    catch( char * str ) {
        //cout << "Exception raised: " << "ololo" << '\n';
        printf("\n Draw sprite failed!\n");
    }
    
    
    
}




//==============================
//UISprite
UISprite::UISprite(int _width, int _height,int _x,int _y, Texture& tex, UISprite* parent):OGLSprite(_width,_height,_x,_y,tex)
{
    childsNum=0;
    relative_x=0;
    relative_y=0;
    
}
UISprite::UISprite(int _width, int _height,int _x,int _y, Texture& tex):OGLSprite(_width,_height,_x,_y,tex)
{
    childsNum=0;
    
    relative_x=0;
    relative_y=0;
    Parent = NULL;
}
//DRAW
void UISprite::Draw()
{
    if(Parent){
        //printf("parent present");
        x=relative_x+(int)Parent->relative_x;
        y=relative_y+(int)Parent->relative_y;    
    }else{
        x=relative_x;
        y=relative_y;
    }
    
    if(x>=-width && y>=-height && x<=640 && y<=480){
        OGLSprite::Draw();//draws at x-y coords
    }
    
    for(int i=0;i<=childsNum-1;i++){
        if(Childs[i]!=nil)Childs[i]->Draw();
    }
    
}




//MOVE
void UISprite::Move(int _x, int _y)
{
    relative_x=_x;
    relative_y=_y;
}
void UISprite::Update(){
    
    OGLSprite::Update();
    for(int i=0;i<=childsNum-1;i++){
        if(Childs[i]!=nil)Childs[i]->Update();
    }

}
//ADD and REMOVE CHILD
void UISprite::AddChild(UISprite* src,int desiredName, UISprite* parent){
    Childs[childsNum] = src;
    Childs[childsNum]->Parent = parent;
    childsNum++;
    
}
void UISprite::StartDrag(int _x,int _y){
    
    if(isDrag==false){
        isDrag = true;
        offsetx = _x - x;
        offsety = _y - y;
    } else {
        Move(_x-offsetx,_y-offsety); 
    }
}


void UISprite::StopDrag(){
    isDrag=false;
}





